/* Copyright (c) 2007 John Wells

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

using Microsoft.Xna.Framework.Content.Pipeline;

namespace GameLevel.ContentPipeline
{
    /// <summary>This class supports defines a linkage between an effect and associated texture parameters and their
    /// values</summary>
    [Serializable, XmlRoot("surface")]
    public sealed class SurfaceDocument
    {

        #region Nested Class: TextureInstanceElement
        public sealed class TextureInstanceElement
        {
            #region Fields
            // Private variables
            private string _assetName;
            private string _filename;
            private string _parameter;
            private string _description;
            #endregion

            #region Properties
            [XmlAttribute("assetName")]
            public string AssetName
            {
                get
                {
                    // Do we have a content pipeline asset name?
                    if (string.IsNullOrEmpty(this._assetName))
                        // No? Build an asset name of folder + file - extension, so 'Effects\Water.fx' gives 'Effects\Water'
                        return Path.Combine(Path.GetDirectoryName(this._filename), Path.GetFileNameWithoutExtension(this._filename));
                    else
                        return this._assetName;
                }
                set { this._assetName = value; }
            }

            [XmlText]
            public string Filename
            {
                get { return this._filename; }
                set { this._filename = value; }
            }

            [XmlAttribute]
            public string Description
            {
                get { return this._description; }
                set { this._description = value; }
            }

            [XmlAttribute("parameter")]
            public string Parameter
            {
                get { return this._parameter; }
                set { this._parameter = value; }
            } 
            #endregion
        } 
        #endregion

         
        #region Fields
        // Private variables
        private string _assetName;
        private string _effect;
        private TextureInstanceElement[] _textureInstances;
        private bool _nodraw = false, _transparent = false, _hidden = false;
        private bool _clip = true;
        private bool _ladder = false;
        private int _lightmapIndex;
        private string _name;
        #endregion

        #region Properties
        [XmlAttribute("assetName")]
        public string AssetName
        {
            get
            {
                // Do we have a content pipeline asset name?
                if (string.IsNullOrEmpty(this._assetName))
                    // No? Build an asset name of folder + file - extension, so 'Effects\Water.fx' gives 'Effects\Water'
                    return Path.Combine(Path.GetDirectoryName(this._effect), Path.GetFileNameWithoutExtension(this._effect));
                else
                    return this._assetName;
            }
            set { this._assetName = value; }
        }

        [XmlAttribute("effect")]
        public string Effect
        {
            get { return this._effect; }
            set { this._effect = value; }
        }

        [XmlElement("texture")]
        public TextureInstanceElement[] TextureInstances
        {
            get { return this._textureInstances; }
            set { this._textureInstances = value; }
        }

        [XmlAttribute("nodraw")]
        public bool NoDraw
        {
            get { return this._nodraw; }
            set { this._nodraw = value; }
        }

        [XmlAttribute("transparent")]
        public bool Transparent
        {
            get { return this._transparent; }
            set { this._transparent = value; }
        }

        [XmlAttribute("hidden")]
        public bool Hidden
        {
            get { return this._hidden; }
            set { this._hidden = value; }
        }


        [XmlAttribute("clip")]
        public bool Clip
        {
            get { return this._clip; }
            set { this._clip = value; }
        }

        [XmlElement("ladder")]
        public bool Ladder
        {
            get { return this._ladder; }
            set { this._ladder = value; }
        }

        public int LightmapIndex
        {
            get { return this._lightmapIndex; }
            set { this._lightmapIndex = value; }
        }

        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }
        #endregion

        #region Methods

        public void Copy(SurfaceDocument surface)
        {
            this._assetName = surface._assetName;
            this._effect = surface._effect;
            this._ladder = surface._ladder;
            this._lightmapIndex = surface._lightmapIndex;
            this._nodraw = surface._nodraw;
            this._textureInstances = surface._textureInstances;
            this._name = surface._name;
        }

        public static SurfaceDocument FromFile(string filename)
        {
            // Attempt to open the given material file for readin'
            using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                // Deserialize the file we found using the built in xml serializer
                XmlSerializer serializer = new XmlSerializer(typeof(SurfaceDocument));
                SurfaceDocument output = (SurfaceDocument)serializer.Deserialize(stream);

                // Validation
                if (output.AssetName == null || output.AssetName.Length == 0) throw new XmlException("AssetName");
                if (output.Effect == null || output.Effect.Length == 0) throw new XmlException("Effect");
                if (output.TextureInstances == null) throw new XmlException("TextureInstances null");
                if (output.TextureInstances.Length == 0) throw new XmlException("TextureInstances 0");
                foreach (TextureInstanceElement instance in output.TextureInstances)
                {
                    if (instance.AssetName == null || instance.AssetName.Length == 0) throw new XmlException("AssetName");
                    if (instance.Filename == null || instance.Filename.Length == 0) throw new XmlException("Filename");
                    if (instance.Parameter == null || instance.Parameter.Length == 0) throw new XmlException("Parameter");
                }

                return output;
            }
        } 
        #endregion
    }
}
